const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trainDate: '',
    trainPositions: [{
        name: '胸肌',
        selected: false
      },
      {
        name: '颈部肌群',
        selected: false
      },
      {
        name: '背阔肌',
        selected: false
      },
      {
        name: '肩部',
        selected: false
      },
      {
        name: '上背肌',
        selected: false
      },
      {
        name: '肱二头肌',
        selected: false
      },
      {
        name: '下背肌',
        selected: false
      },
      {
        name: '肱三头肌',
        selected: false
      },
      {
        name: '腹肌',
        selected: false
      },
      {
        name: '前臂',
        selected: false
      },
      {
        name: '侧腹肌',
        selected: false
      },
      {
        name: '手腕',
        selected: false
      },
      {
        name: '臀部肌群',
        selected: false
      },
      {
        name: '膝关节',
        selected: false
      },
      {
        name: '股四头肌',
        selected: false
      },
      {
        name: '小腿肌群',
        selected: false
      },
      {
        name: '大腿后侧肌',
        selected: false
      },
      {
        name: '脚踝',
        selected: false
      },
      {
        name: '脊柱',
        selected: false
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const trainDate = wx.getStorageSync("trainDate");
    const trainPositionsJson = wx.getStorageSync("trainPositions");
    const selectPositions = JSON.parse(trainPositionsJson) || [];
    const trainPositions = this.data.trainPositions;
    trainPositions.forEach(function(item) {
      if (selectPositions.indexOf(item.name) >= 0) {
        item.selected = true;
      }
    });
    this.setData({
      trainPositions: trainPositions,
      trainDate: trainDate
    });
  },

  changeSelect: function(e) {
    const index = e.currentTarget.dataset.index;
    const trainPositions = this.data.trainPositions;
    trainPositions[index].selected = !trainPositions[index].selected;
    this.setData({
      trainPositions: trainPositions
    });
  },
  savePosition: function() {
    const selectPositionArr = [];
    const trainPositions = this.data.trainPositions;
    trainPositions.forEach(function(item) {
      if (item.selected) {
        selectPositionArr.push(item.name);
      }
    });
    const url = app.globalData.url;
    const openId = app.globalData.openId;
    const trainDate = this.data.trainDate;
    const selectPositions = selectPositionArr.join(',');
    wx.request({
      url: url + '/train/savePositions',
      data: {
        openId: openId,
        trainDate: trainDate,
        trainPositions: selectPositions
      },
      method: 'POST',
      success: function(response) {
        if (response.data.success) {
          //保存 
          wx.navigateBack({
            delta: 1
          });
          wx.showToast({
            title: '保存成功',
            icon: 'none'
          });
        }else{
          wx.showToast({
            title: '保存失败:' + response.data.message,
            icon: 'none'
          })
        }
      }
    })
  }
})